/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.ui.bgproc;

import sk.jp.commons.tools.Messages;
import sk.jp.commons.utils.BackgroundProcess;
import sk.jp.commons.ui.StatusBar;

/**
 * Handles background process state chnges and presents it as message on status bar.
 * <p>See package description for additional information.
 */
public class ProcessStateHandlerForStatusbar implements BackgroundProcess.StateHandler{

    public enum Msg {
        started("ui.processstatehandlerforstatusbar.running"),//Running
        cancelling("ui.processstatehandlerforstatusbar.cancelling"),//Cancelling
        cancelled("ui.processstatehandlerforstatusbar.cancelled"),//Cancelled
        finished("ui.processstatehandlerforstatusbar.finished"),//Finished
        finishedErr("ui.processstatehandlerforstatusbar.finishedError"),//Failed
        ;
        //----------------------------------------------------------------------
        private String propKey;
        Msg(String propKey) {
            this.propKey = propKey;
        }
        public String getPropKey() {
            return propKey;
        }
        public String value() {
            return Messages.getMessage(propKey);
        }
    }

    //----------------------------------------------------------------------

    private final StatusBar statusBar;
    private String processStartedMessage;
    private String processCancelRequestMessage;
    private String processCancelledMessage;
    private String processFinishedMessage;
    private String processFinishedWithErrorMessage;

    //----------------------------------------------------------------------

    public ProcessStateHandlerForStatusbar(StatusBar statusBar) {
        this(statusBar, Msg.started.value(), Msg.cancelling.value(),
             Msg.cancelled.value(), Msg.finished.value(), Msg.finishedErr.value());
    }

    public ProcessStateHandlerForStatusbar(StatusBar statusBar,
                                          String processStartedMessage,
                                          String processCancelRequestMessage,
                                          String processCancelledMessage,
                                          String processFinishedMessage,
                                          String processFinishedWithErrorMessage) {
        this.statusBar = statusBar;
        this.processStartedMessage = processStartedMessage;
        this.processCancelRequestMessage = processCancelRequestMessage;
        this.processCancelledMessage = processCancelledMessage;
        this.processFinishedMessage = processFinishedMessage;
        this.processFinishedWithErrorMessage = processFinishedWithErrorMessage;
    }

    //----------------------------------------------------------------------

    public void processStarted(String processInfo) {
        statusBar.setMessage(processStartedMessage + " " + processInfo + " ...");
    }

    public void processCancelRequest(String processInfo) {
        statusBar.setMessage(processCancelRequestMessage + " " + processInfo);
    }

    public void processCancelled(String processInfo) {
        statusBar.setMessage(processCancelledMessage + " " + processInfo);
    }

    public void processFinished(String processInfo) {
        statusBar.setMessage(processFinishedMessage + " " + processInfo);
    }

    public void processFinishedWithError(String processInfo, Throwable t) {
        statusBar.setMessage(processFinishedWithErrorMessage + " " + processInfo);
    }
}
